<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>公众号用户管理</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/font-awesome.min.css" media="all">
    <style>
        .layui-form-switch {
            width: 54px;
            margin-top: 0;
        }

        .layui-form-switch i {
            top: 2px;
        }
    </style>
</head>
<body>
<div style="padding: 20px; position: relative;">
    <div class="operateTable">
        微信昵称：
        <div class="layui-inline">
            <input class="layui-input" name="NICK_NAME" id="NICK_NAME" placeholder="请输入关键字" autocomplete="off">
        </div>
        合同签订人：
        <div class="layui-inline">
            <input class="layui-input" name="SIGNER" id="SIGNER" placeholder="请输入关键字" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
    <table class="layui-table" lay-data="{height:'full-110', url:'/user/weixin_user/', page:true, limit:20, id:'roleTable'}"
           lay-filter="role">
        <thead>
        <tr>
            <!--<th lay-data="{checkbox:true, fixed: true}"></th>-->
            <th lay-data="{field:'NICK_NAME', width:250 ,align: 'center'} ">微信昵称</th>
            <th lay-data="{field:'OPERA_TIME', width:250 ,align: 'center'}">微信头像</th>
            <th lay-data="{field:'SIGNER', width:300 ,align: 'center'}">合同签订人</th>
            <!--<th lay-data="{field:'OPEN_ID', width:350 ,align: 'center'}">合同</th>-->
            <th lay-data="{field:'IS_OPEN', templet:'#statusTpl', width:150, align:'center'}">是否开启</th>

        </tr>
        </thead>
    </table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-mini" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-mini" lay-event="del">删除</a>
    </script>
</div>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    var table;
    layui.use(['jquery', 'table', 'form', 'layer','laydate'], function () {
        table = layui.table;
        var form = layui.form;
        var $ = layui.jquery;
        var laydate = layui.laydate;

        laydate.render({
            elem: '#OPERA_TIME'
            , format: 'yyyy-MM-dd'
        });

        var active = {
            reload: function () {
                var NICK_NAME = $('#NICK_NAME').val();
                var SIGNER = $('#SIGNER').val();
                table.reload('roleTable', {
                    where: {
                        NICK_NAME: NICK_NAME,
                        SIGNER:SIGNER
                    }
                });
            },
            addRole: function () {
                layer.open({
                    type: 2,
                    title: '新增角色',
                    shadeClose: false,
                    shade: [0.3],
                    maxmin: true, //开启最大化最小化按钮
                    area: ['900px', '90%'],
                    scrollbar: false, //屏蔽滚动条
                    content: 'operate.html?type=add'
                });
            }
        };

        $('.operateTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条
        table.on('tool(role)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                layer.open({
                    type: 2,
                    title: "编辑 " + data.ROLE_NAME,
                    shadeClose: false,
                    shade: [0.3],
                    maxmin: true, //开启最大化最小化按钮
                    area: ['900px', '90%'],
                    scrollbar: false, //屏蔽滚动条
                    content: 'operate.html?type=edit&id=' + data.ID
                });
            }
        });

        //监听指定开关
        form.on('switch(switchTest)', function(data){
            var ID = data.othis.parent().find("input[name='IS_OPEN']").val();
            var name = data.othis.parent().find("input[name='IS_OPEN']").data("name");
            var open = "1";
            if (this.checked === true){
                open = "1";
            } else if (this.checked === false){
                open = "0";
            }
            $.ajax({
                url: "/user/weixin_open",
                data: {ID: ID, IS_OPEN: open},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.errcode === "0"){
                        reloadTable();    // 表格刷新
                        if (open === "1"){
                            layer.msg(name + ' 已启用', {offset: '150px'});
                        } else if (open === "0"){
                            layer.msg(name + ' 已关闭', {offset: '150px'});
                        }
                    } else if (res.errcode === "1"){
                        layer.msg(res.errmsg, function(){});
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        });


    });

    function reloadTable() {
        table.reload('roleTable', {});
    }
</script>
<script type="text/html" id="statusTpl">
    {{#  if(d.IS_OPEN == "1"){ }}
    <input type="checkbox" checked="" value="{{d.OPEN_ID}}" data-name="{{d.NICK_NAME}}" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">
    {{#  } else if(d.IS_OPEN == "0"){ }}
    <input type="checkbox" value="{{d.OPEN_ID}}" data-name="{{d.NICK_NAME}}" name="IS_OPEN" lay-skin="switch" lay-filter="switchTest" lay-text="启用|关闭">
    {{#  } else { }}
    异常
    {{#  }}}
</script>
</body>
</html>